#!/bin/bash

#================================================
# Verifico parametros

ruta=`dirname $0`
lista="h102|h104|h106|h108|h114|h116|h122|hosta|hostb|hostc|web|ftp|telnet"

if [ $# -lt 1 ]; then
	echo "Configura un host para una red"
	echo ""
	echo "Uso: `basename $0`  <host>  [nullmodem | modem [llamar | atender]]"
	echo ""
	echo "     donde <host> = $lista"
	echo "           nullmodem = para conectarse por nullmodem"
	echo "           modem llamar | atender = para conectarse por modem, llamar o atender"
	echo ""
	exit 1
fi

case $1 in
h102|h104|h106|h108|h114|h116|h122|hosta|hostb|hostc|web|ftp|telnet)
	;;
*)
	echo "!! El host puede ser solamente uno de estos:"
	echo "$lista"
	exit 2
	;;
esac

#================================================
# Hago backups

if [ ! -e $ruta/backups ]; then
	mkdir $ruta/backups

	echo "@@ Haciendo backup de interfaces"
	ifconfig >$ruta/backups/ifconfig

	echo "@@ Haciendo backup de rutas"
	ip route show all >$ruta/backups/route

	echo "@@ Haciendo backup de /etc/hosts"
	cp /etc/hosts $ruta/backups/hosts

	echo "@@ Haciendo backup de /proc/sys/net/ipv4/ip_forward"
	cp /proc/sys/net/ipv4/ip_forward $ruta/backups/ip_forward
fi

#================================================
# Borro tablas, interfaces, cache

echo "@@ Borrando /etc/hosts"
echo "# Tabla vacia a proposito" >/etc/hosts

echo "@@ Reseteando interfaces y rutas"
ip route flush all

for placa in `ifconfig | grep -e "^eth0:[[:digit:]]" | cut -f1 -d" "`; do
	ifconfig $placa down
done

for placa in `ifconfig | grep -e "^eth" | cut -f1 -d" "`; do
	ifconfig $placa down
done

echo "@@ Reseteando ARP cache"
ip neigh flush dev eth0

#================================================
# Mato dhcp y pppd

echo "@@ Matando dhcp"
killall -9 dhcpcd

echo "@@ Matando pppd"
killall -9 pppd

#================================================
# Activo forwarding si es un router

#router=0
if [[ `echo "$1" | sed -e "s/\(..\).*/\1/"` == "h1" ]]; then
	echo "@@ Activando ip forward"
	echo "1" >/proc/sys/net/ipv4/ip_forward
	#router=1
else
	echo "@@ Desactivando ip forward"
	echo "0" >/proc/sys/net/ipv4/ip_forward
	#router=0
fi

#================================================
# Rutinas del modem

modem=0
if [[ $2 = "modem" ]]; then
	if [[ $3 = "llamar" ]]; then
		comandoppp="pppd updetach noauth debug /dev/ttyS0 9600 nodefaultroute \
		connect \"/usr/sbin/chat -v -f $ruta/modem/llamar\" "
	elif [[ $3 = "atender" ]]; then
		comandoppp="pppd updetach noauth debug /dev/ttyS0 9600 nodefaultroute \
		connect \"/usr/sbin/chat -v -f $ruta/modem/atender\" "
	else
		echo "!! ERROR: Falta especificar 'llamar' o 'atender'"
		exit 3
	fi
	modem=1
elif [[ $2 = "nullmodem" ]]; then
	comandoppp="pppd updetach nodefaultroute crtscts lock /dev/ttyS0 38400 local "
	modem=1
fi

echo "@@ Todo limpiado"
echo "@@--------------"

#================================================
# Configuro router o hosts

echo "@@ Configurando $1:"
#if [ $router -eq 1 ]; then

	echo "$comandoppp" > $ruta/modem/comandoppp
	chmod +x $ruta/modem/comandoppp
	
	. $ruta/bin/$1.sh $modem
	
# else
	# case $1 in
	# ha)
		# ifconfig eth0 10.34.1.70 netmask 255.255.255.224 broadcast 10.34.1.95 up
		# ;;
	# hb)
		# ifconfig eth0 10.9.2.200 netmask 255.255.255.192 broadcast 10.9.2.255 up
		# ;;
	# hc)
		# ifconfig eth0 10.34.1.20 netmask 255.255.255.240 broadcast 10.34.1.31 up
		# ;;
	# web)
		# ifconfig eth0 192.168.3.1 netmask 255.255.255.0 broadcast 192.168.3.255 up
		# ;;
	# ftp)
		# ifconfig eth0 10.11.2.1 netmask 255.255.255.0 broadcast 10.11.2.255 up
		# ;;
	# telnet)
		# ifconfig eth0   10.34.13.129 netmask 255.255.255.128 broadcast 10.34.13.143 up
		# ifconfig eth0:0 10.34.1.130  netmask 255.255.255.240 broadcast 10.34.1.255 up
		# ;;
	# esac
#fi

if [ $? -eq 0 ]; then
	echo "@@ Todo OK"
else
	echo "!! ERROR en la configuracion"
fi
